<div class="easyui-tabs" data-options="fit:true,plain:true">
    <div title="巡检任务">
        <table class="easyui-datagrid" id="operation_checkcenter"  pagination="true" pageSize="50"
               data-options="fitColumns:true,fit:true,singleSelect:false,rownumbers:true"
               toolbar="#operation-checkcenter-datagrid-toolbar" url="/admin/operation/checkcenter" width="100%">
            <thead>
            <th data-options="field:'ck',checkbox:true"></th>
            <th data-options="field:'code',sortable:false,align:'center',width:100">任务编号</th>
            <th data-options="field:'name',sortable:false,align:'center',width:100">任务名称</th>
            <th data-options="field:'start_date',sortable:false,align:'center',width:100">开始日期</th>
            <th data-options="field:'stop_date',sortable:false,align:'center',width:100">结束日期</th>
            <th data-options="field:'user',sortable:false,align:'center',width:100">巡检人</th>
            <th data-options="field:'state',sortable:false,align:'center',width:100">任务状态</th>
            <th data-options="field:'id',sortable:false,align:'center',formatter:OperationModule.operate,width:100">
                管理员操作
            </th>
            </thead>
        </table>
    </div>
    <!--<div title="巡检配置">-->
        <!--<table class="easyui-datagrid" id="operation_checkconf"-->
               <!--data-options="fitColumns:true,fit:true,singleSelect:true,rownumbers:true"-->
               <!--toolbar="#operation-checkconf-datagrid-toolbar" url="/admin/operation/checkconf">-->
            <!--<thead>-->
            <!--<th data-options="field:'code',sortable:false,align:'center',width:100">设备编号</th>-->
            <!--<th data-options="field:'area',sortable:false,align:'center',width:100">巡检区域</th>-->
            <!--<th data-options="field:'cycle',sortable:false,align:'center',width:100">巡检周期</th>-->
            <!--<th data-options="field:'id',sortable:false,align:'center',width:100,formatter:OperatioCheckConfnModule.operate">-->
                <!--管理员操作-->
            <!--</th>-->
            <!--</thead>-->
        <!--</table>-->
    <!--</div>-->
</div>
<div id="operation-checkcenter-datagrid-toolbar" style="padding:5px;height:auto">
    <form style="margin: 10px;">
        <label>任务时间:</label>
        <input id="start_date" class="easyui-datebox" style="width:100px">-<input id="end_date" class="easyui-datebox"
                                                                                  style="width:100px">
        <label for="keyword">关键字：</label>
        <input type="text" id="keyword" class="easyui-textbox" style="width:200px">
        <select class="easyui-combobox" id="key_type">
            <option value="-1">全部</option>
            <option value="1">任务编号</option>
            <option value="2">巡检人</option>
        </select>
        <span class="easyui-linkbutton" onclick="checked(1)"><input type="checkbox" id="no_assign"
                                                                    style="height: 13px;vertical-align:text-top; margin-top:2px;">未分配</span>
        <span class="easyui-linkbutton" onclick="checked(2)"><input type="checkbox"
                                                                    style="height: 13px;vertical-align:text-top; margin-top:2px;"
                                                                    id="no_executed">未执行</span>
        <span class="easyui-linkbutton" onclick="checked(3)"><input type="checkbox"
                                                                    style="height: 13px;vertical-align:text-top; margin-top:2px;"
                                                                    id="executed">已执行</span>
        <span class="easyui-linkbutton" onclick="checked(4)"><input type="checkbox"
                                                                    style="height: 13px;vertical-align:text-top; margin-top:2px;"
                                                                    id="ignore">已忽略</span>
        <a href="javascript:void(0);" onclick="OperationModule.search(this);" class="easyui-linkbutton"
           iconCls="icons-table-table">查询</a>
        <br/>
        <span class="easyui-linkbutton toolbar-btn__hide" style="margin: 10px;"
              data-options="plain:false,iconCls:'icons-table-table_delete'"
              onclick="OperationModule.hide('a')">忽略</span>
        <a href="javascript:void(0);" class="easyui-linkbutton"
           data-options="plain:true,iconCls:'icons-table-table_refresh'" onclick="OperationModule.refresh()">刷新</a>
    </form>
</div>

<div id="operation-checkconf-datagrid-toolbar" style="padding:5px;height:auto">
    <form style="margin: 10px;">
        <label for="ckeyword">关键字：</label>
        <input type="text" id="ckeyword" class="easyui-textbox" style="width:200px">
        <select class="easyui-combobox" id="ckey_type">
            <option value="-1">全部</option>
            <option value="1">设备编号</option>
            <option value="2">负责人</option>
        </select>
        <label for="cycle">巡检周期：</label>
        <select class="easyui-combobox" id="cycle" style="width: 100px;">
            <option value="-1">全部</option>
            <option value="1">30</option>
            <option value="2">60</option>
        </select>
        <a href="javascript:void(0);" onclick="OperatioCheckConfnModule.search(this);" class="easyui-linkbutton"
           iconCls="icons-table-table">查询</a>
        <br/>
        <a href="javascript:void(0);" class="easyui-linkbutton"
           data-options="plain:true,iconCls:'icons-table-table_refresh'"
           onclick="OperatioCheckConfnModule.refresh()">刷新</a>
    </form>
</div>

<script type="text/javascript">

    function checked(v) {
        if (v == 1) {
            if ($("#no_assign").is(':checked') == true) {
                $("#no_assign").val(0);
                $("#no_assign").prop("checked", false);
            } else {
                $("#no_assign").val(1);
                $("#no_assign").prop("checked", true);
            }
        }
        if (v == 2) {
            if ($("#no_executed").is(':checked') == true) {
                $("#no_executed").val(0);
                $("#no_executed").prop("checked", false);
            } else {
                $("#no_executed").val(1);
                $("#no_executed").prop("checked", true);
            }
        }
        if (v == 3) {
            if ($("#executed").is(':checked') == true) {
                $("#executed").val(0);
                $("#executed").prop("checked", false);
            } else {
                $("#executed").val(1);
                $("#executed").prop("checked", true);
            }
        }
        if (v == 4) {
            if ($("#ignore").is(':checked') == true) {
                $("#ignore").val(0);
                $("#ignore").prop("checked", false);
            } else {
                $("#ignore").val(1);
                $("#ignore").prop("checked", true);
            }
        }
    }
    
    var OperationModule = {
        dialog: '#globel-dialog-div',
        datagrid: '#operation_checkcenter',
        hideBtn:'.toolbar-btn__hide',

        checkSelectorLength:function(){
            var checkedItems = $(this.datagrid).datagrid('getChecked');
            var hideBtn = $(this.hideBtn);
            if(checkedItems.length){
                hideBtn.removeClass('is-disabled');
            }else{
                hideBtn.hasClass('is-disabled') || hideBtn.addClass('is-disabled');
            }
        },
        //操作格式化
        operate: function (val) {
            var btn = [];
            btn.push('<a href="javascript:void(0);" onclick="OperationModule.permission(' + val + ')">分配</a>');
            btn.push('<a href="javascript:void(0);" onclick="OperationModule.hide(' + val + ')">忽略</a>');
            return btn.join(' | ');
        },

        permission: function (index) {
            var href = "<{:U('admin/operation/to_user')}>";
            href += href.indexOf('?') != -1 ? '&id=' + index : '?id=' + index;
            var that = this;
            $(that.dialog).dialog({
                title: '分配人员',
                iconCls: 'icons-application-application_edit',
                width: 400,
                height: 250,
                cache: false,
                href: href,
                modal: true,
                collapsible: false,
                minimizable: false,
                resizable: false,
                maximizable: false,
                buttons: [{
                    text: '确定',
                    iconCls: 'icons-other-tick',
                    handler: function () {
                        var formElement = document.getElementById("to_user");
                        var formData = new FormData(formElement);
                        var action = "<{:U('admin/operation/to_user?dosubmit=1')}>";
                        $(that.dialog).find('form').eq(0).form('submit', {
                            onSubmit: function () {
                                $.ajax({
                                    type: "POST",
                                    url: action,
                                    enctype: 'multipart/form-data',
                                    data: formData,
                                    success: function (res) {
                                        if (res.code != '0000') {
                                            $.app.method.tip('提示信息', res.info, 'error');
                                        }
                                        else {
                                            $.app.method.tip('提示信息', res.info, 'info');
                                            $(that.dialog).dialog('close');
                                            that.refresh();
                                        }
                                    },
                                    cache: false,
                                    contentType: false,
                                    processData: false
                                });
                                return false;
                            }
                        });
                    }
                }, {
                    text: '取消',
                    iconCls: 'icons-arrow-cross',
                    handler: function () {
                        $(that.dialog).dialog('close');
                    }
                }]
            });
        },

        hide: function (v) {
            var id ='';
            if (v == 'a') {
                var checkedItems = $(this.datagrid).datagrid('getChecked');
                if(checkedItems.length <= 0) return;
                
                var ids = [];
                $.each(checkedItems, function (index, item) {
                    ids.push(item.id);
                });
                 id = ids.join(",");
            } else {
                 id = v;
            }
            var that = this;
            $.messager.confirm('提示信息', '确定要忽略吗？', function (result) {
                if (!result) return false;
                $.messager.progress({text: '处理中，请稍候...'});
                $.post("<{:U('admin/operation/updateCheckTask')}>", {id: id}, function (res) {
                    $.messager.progress('close');
                    if (!res.status) {
                        $.app.method.tip('提示信息', res.info, 'error');
                    } else {
                        $.app.method.tip('提示信息', res.info, 'info');
                        that.refresh();
                    }
                }, 'json');
            });
        },
        //刷新
        refresh: function () {
            $(this.datagrid).datagrid('reload');
        },
        //数据查询
        search: function () {
            var no_assign = $("#no_assign").val();
            var no_executed = $("#no_executed").val();
            var executed = $("#executed").val();
            var ignore = $("#ignore").val();
            var end_date = $("#end_date").combobox('getValue');
            var start_date = $("#start_date").combobox('getValue');
            var key_type = $("#key_type").combobox('getValue');
            var keyword = $("#keyword").val();
            $(this.datagrid).datagrid("load", {
                no_assign: no_assign,
                no_executed: no_executed,
                executed: executed,
                ignore: ignore,
                start_date: start_date,
                end_date: end_date,
                key_type: key_type,
                keyword: keyword
            });
        },
    };
    var OperatioCheckConfnModule = {
        dialog: '#globel-dialog-div',
        datagrid: '#operation_checkconf',
        //操作格式化
        operate: function (val) {
            var btn = [];
            btn.push('<a href="javascript:void(0);" onclick="OperatioCheckConfnModule.edit(' + val + ')">编辑</a>');
            return btn.join(' | ');
        },

        edit: function (index) {
            var href = "<{:U('admin/operation/checkEdit')}>";
            href += href.indexOf('?') != -1 ? '&id=' + index : '?id=' + index;
            var that = this;
            $(that.dialog).dialog({
                title: '编辑配置',
                iconCls: 'icons-application-application_edit',
                width: 400,
                height: 300,
                cache: false,
                href: href,
                modal: true,
                collapsible: false,
                minimizable: false,
                resizable: false,
                maximizable: false,
                buttons: [{
                    text: '确定',
                    iconCls: 'icons-other-tick',
                    handler: function () {
                        var formElement = document.getElementById("check_edit");
                        var formData = new FormData(formElement);
                        var action = "<{:U('admin/operation/checkEdit?dosubmit=1')}>";
                        $(that.dialog).find('form').eq(0).form('submit', {
                            onSubmit: function () {
                                $.ajax({
                                    type: "POST",
                                    url: action,
                                    enctype: 'multipart/form-data',
                                    data: formData,
                                    success: function (res) {
                                        if (res.code != '0000') {
                                            $.app.method.tip('提示信息', res.info, 'error');
                                        }
                                        else {
                                            $.app.method.tip('提示信息', res.info, 'info');
                                            $(that.dialog).dialog('close');
                                            that.refresh();
                                        }
                                    },
                                    cache: false,
                                    contentType: false,
                                    processData: false
                                });
                                return false;
                            }
                        });
                    }
                }, {
                    text: '取消',
                    iconCls: 'icons-arrow-cross',
                    handler: function () {
                        $(that.dialog).dialog('close');
                    }
                }]
            });
        },

        //刷新
        refresh: function () {
            $(this.datagrid).datagrid('reload');
        },
        //数据查询
        search: function () {
            var no_assign = $("#no_assign").val();
            var no_executed = $("#no_executed").val();
            var executed = $("#executed").val();
            var ignore = $("#ignore").val();
            var keyword = 111;
            $(this.datagrid).datagrid("load", {
                no_assign: no_assign,
                no_executed: no_executed,
                executed: executed,
                ignore: ignore,
                keyword: keyword
            });
        }

    };
    $('#operation_checkcenter').datagrid({
        onLoadSuccess:function(data){
            OperationModule.checkSelectorLength();
        },
        onCheck:function(rowIndex, rowData){
            OperationModule.checkSelectorLength();
        },
        onUncheck:function(rowIndex, rowData){
            OperationModule.checkSelectorLength();
        },
        onCheckAll:function(rowIndex, rowData){
            OperationModule.checkSelectorLength();
        },
        onUncheckAll:function(rowIndex, rowData){
            OperationModule.checkSelectorLength();
        },
    })
</script>